package com.scrcu.cloud.wechat.home.endpoint.common.utils;

import com.scrcu.cloud.wechat.home.endpoint.common.exception.BusiException;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @auther: siliang liu
 * @date: 2024/4/23 17:16
 * @description: 文件工具类
 * @version: 1.0.0
 */
@Slf4j
public class FileUtil {

    /**
     * 获取文件类型
     *
     * @param inputPart 文件
     * @return 文件类型
     */
    public static String fileType(InputPart inputPart) {
        return inputPart.getMediaType().getSubtype();
    }

    /**
     * 获取文件类型
     *
     * @param parts 文件
     * @return 文件类型
     */
    public static String fileType(List<InputPart> parts) {
        return parts.get(0).getMediaType().getSubtype();
    }

    /**
     * 获取文件类型
     *
     * @param file     文件
     * @param fileName 文件名
     * @return 文件类型
     */
    public static String fileType(MultipartFormDataInput file, String fileName) {
        return file.getFormDataMap().get(fileName).get(0).getMediaType().getSubtype();
    }

    /**
     * 获取文件流
     *
     * @param file     文件
     * @param fileName 文件名
     * @return 文件类型
     */
    public static InputStream getInputStream(MultipartFormDataInput file, String fileName) throws IOException {
        return file.getFormDataMap().get(fileName).get(0).getBody(InputStream.class, null);
    }

    /**
     * 压缩图片
     */
    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
        if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
            return imageBytes;
        }
        long srcSize = imageBytes.length;
        double accuracy = getAccuracy(srcSize / 1024);
        log.info("<===============图片压缩开始================>");
        try {
            while (imageBytes.length > desFileSize * 1024) {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
                Thumbnails.of(inputStream)
                        .scale(accuracy)
                        .outputQuality(accuracy)
                        .toOutputStream(outputStream);
                imageBytes = outputStream.toByteArray();
            }
            log.info("<===============图片压缩结束================>");
            log.info("图片原大小=" + srcSize / 1024 + "kb,压缩后大小=" + imageBytes.length / 1024 + "kb");
        } catch (Exception e) {
            log.error("【图片压缩】msg=图片压缩失败! {}", e.getMessage());
            throw new BusiException("EBWX999", "图片压缩失败");
        }
        return imageBytes;
    }

    /**
     * 精度
     */
    private static double getAccuracy(long size) {
        double accuracy;
        if (size < 900) {
            accuracy = 0.85;
        } else if (size < 2047) {
            accuracy = 0.6;
        } else if (size < 3275) {
            accuracy = 0.44;
        } else {
            accuracy = 0.4;
        }
        return accuracy;
    }
}

